﻿#include "tool_xml.h"
#include "work_tcp.h"
#include "localDev.h"
#include "motor.h"
#include "hmi.h"
#include <QDebug>
#include <QDomDocument>
#include <QDateTime>

ToolXml::ToolXml()
{

}

QString ToolXml::makeLoadXml(QString sn)
{
    /* 检验sn */
    if (sn.size() == 0)
    {
        qDebug() << "当前sn为空！";
        sn = "00001";
    }

    QDomDocument doc;
    QDomProcessingInstruction head =
            doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(head);

    // 根节点
    QDomElement tree = doc.createElement("LOGIN_REQ");
    doc.appendChild(tree);

    // 子节点
    tree.appendChild(addElement("id", sn));

    return doc.toString();
}

QString ToolXml::makeHeartbeatXml(QString sn)
{
    /* 检验sn */
    if (sn.size() == 0)
    {
        qDebug() << "当前sn为空！";
        sn = "00001";
    }

    QDomDocument doc;
    QDomProcessingInstruction head =
            doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(head);

    // 根节点
    QDomElement tree = doc.createElement("HB_REQ");
    doc.appendChild(tree);

    // 子节点
    tree.appendChild(addElement("id", sn));
    tree.appendChild(addElement("time", QDateTime::currentDateTime().toString("yyyyMMddhhmmss")));

    return doc.toString();
}

int ToolXml::readHeartbeatXml(QString xml)
{
    int ret;
    QDomDocument doc;
    if(!doc.setContent(xml))
    {
        qDebug() << "xml加载失败！";
        return false;
    }

    //返回根节点
    QDomElement root = doc.documentElement();
    if (root.tagName() != "HB_RES")
    {
        qDebug() << "HB_RES 根节点不匹配！接收到的为：" + root.tagName();
        return 1;
    }

    //获得第一个子节点
    QDomNode node = root.firstChild();
    while(!node.isNull())  // 如果节点不空
    {
        if(node.isElement()) // 如果节点是元素
        {
            QDomElement e = node.toElement(); // 转换为元素，注意元素和节点是两个数据结构，其实差不多
            if (e.tagName() == "ret")
            {
                ret = e.text().toInt();
            }
        }
        node = node.nextSibling(); // 下一个兄弟节点,nextSiblingElement()是下一个兄弟元素，都差不多
    }

    return ret;
}

QString ToolXml::makeGetClusterXml(QString sn)
{
    /* 检验sn */
    if (sn.size() == 0)
    {
        qDebug() << "当前sn为空！";
        sn = "00001";
    }

    QDomDocument doc;
    QDomProcessingInstruction head =
            doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(head);

    // 根节点
    QDomElement tree = doc.createElement("CLUSTER_CONFIG_GET_REQ");
    doc.appendChild(tree);

    // 子节点
    tree.appendChild(addElement("sn", sn));

    return doc.toString();
}

/**
 * @brief ToolXml::readGetClusterXml 解析簇控制器信息
 * @param xml
 * @return
 */
int ToolXml::readGetClusterXml(QString xml, Cluster * c)
{
    int ret = 1;
    QDomDocument doc;
    if(!doc.setContent(xml))
    {
        qDebug() << "xml加载失败！";
        return false;
    }

    //返回根节点
    QDomElement root = doc.documentElement();
    if (root.tagName() != "CLUSTER_CONFIG_GET_RES")
    {
        qDebug() << "CLUSTER_CONFIG_GET_RES 根节点不匹配！接收到的为：" + root.tagName();
        return 1;
    }

    //获得第一个子节点
    QDomNode node = root.firstChild();
    while(!node.isNull())  // 如果节点不空
    {
        if(node.isElement()) // 如果节点是元素
        {
            QDomElement e = node.toElement(); // 转换为元素，注意元素和节点是两个数据结构，其实差不多

            if (e.tagName() == "sn") {
                c->setSn(e.text());
            } else if (e.tagName() == "ret") {
                ret = e.text().toInt();
            } else if (e.tagName() == "hwver") {
                c->setHwVer(e.text());
            } else if (e.tagName() == "swver") {
                c->setSwVer(e.text());
            } else if (e.tagName() == "swbtime") {
                c->setSwbTime(e.text());
            } else if (e.tagName() == "loglevel") {
                c->setLogLevel(e.text().toInt());
            } else if (e.tagName() == "switch") {
                c->setSwitchId(e.text());
            } else if (e.tagName() == "line") {
                c->setLine(e.text());
            } else if (e.tagName() == "line_id") {
                c->setLineId(e.text());
            } else if (e.tagName() == "iptype") {
                c->setDhcp(e.text().toInt());
            } else if (e.tagName() == "ipaddr") {
                c->setIp(e.text());
            } else if (e.tagName() == "netmask") {
                c->setMask(e.text());
            } else if (e.tagName() == "gateway") {
                c->setGw(e.text());
            } else if (e.tagName() == "dns0") {
                c->setDns1(e.text());
            } else if (e.tagName() == "dns1") {
                c->setDns2(e.text());
            } else if (e.tagName() == "lcnum") {
                c->setLocalDevCnt(e.text().toInt());
            }
        }
        node = node.nextSibling(); // 下一个兄弟节点,nextSiblingElement()是下一个兄弟元素，都差不多
    }


    QDomNodeList addr  = doc.elementsByTagName("maddr");
    QDomNodeList bus  = doc.elementsByTagName("busid");
    QDomNodeList offset = doc.elementsByTagName("offset");

    for (int i = 0; i < addr.size(); i++)
    {
        LocalDev local;
        QDomElement eAddr   = addr.at(i).toElement();
        QDomElement eBus    = bus.at(i).toElement();
        QDomElement eOffset = offset.at(i).toElement();
        local.setMAddr(eAddr.text());
        local.setBusID(eBus.text().toInt());
        local.setOffset(eOffset.text().toInt());
        c->m_localDev.append(local);

        qDebug() << QString("Cluster[%1] --> maddr = %2, bus = %3, offset = %4!").arg(i).arg(local.getMAddr()).arg(local.busID()).arg(local.offset());
    }

    return ret;
}

QString ToolXml::makeGetLocalXml(QString sn, int busId, QString maddr)
{
    QDomDocument doc;
    QDomProcessingInstruction head =
            doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(head);

    // 根节点
    QDomElement tree = doc.createElement("LCONTROLLER_CONFIG_GET_REQ");
    doc.appendChild(tree);

    // 子节点
    tree.appendChild(addElement("sn", sn));
    tree.appendChild(addElement("maddr", maddr));
    tree.appendChild(addElement("busid", QString::number(busId)));

    return doc.toString();
}

int ToolXml::readGetLocalXml(QString xml, LocalDev * ld)
{
    ld->m_motor.resize(0);
    int ret = 1;
    QDomDocument doc;
    if(!doc.setContent(xml))
    {
        qDebug() << "xml加载失败！";
        return false;
    }

    //返回根节点
    QDomElement root = doc.documentElement();
    if (root.tagName() != "LCONTROLLER_CONFIG_GET_RES")
    {
        qDebug() << "LCONTROLLER_CONFIG_GET_RES 根节点不匹配！接收到的为：" + root.tagName();
        return 1;
    }

    //获得第一个子节点
    QDomNode node = root.firstChild();
    while(!node.isNull())  // 如果节点不空
    {
        if(node.isElement()) // 如果节点是元素
        {
            QDomElement e = node.toElement(); // 转换为元素，注意元素和节点是两个数据结构，其实差不多

            if (e.tagName() == "hwver") {
                ld->setHwVer(e.text());
            } else if (e.tagName() == "swver") {
                ld->setSwVer(e.text());
            } else if (e.tagName() == "uid") {
                ld->setUid(e.text());
            } else if (e.tagName() == "swbtime") {
                ld->setSwbTime(e.text());
            } else if (e.tagName() == "loglevel") {
                ld->setLogLevel(e.text().toInt());
            } else if (e.tagName() == "baudrate") {
                ld->setBaudrate(e.text().toInt());
            } else if (e.tagName() == "row") {
                ld->setRow(e.text().toInt());
            } else if (e.tagName() == "col") {
                ld->setCol(e.text().toInt());
            } else if (e.tagName() == "sector") {
                ld->setSector(e.text().toInt());
            } else if (e.tagName() == "hcdi") {
                ld->setHcdi(e.text().toInt());
            } else if (e.tagName() == "longitude") {
                ld->setLongitude(e.text().toDouble());
            } else if (e.tagName() == "latitude") {
                ld->setLatitude(e.text().toDouble());
            } else if (e.tagName() == "altitude") {
                ld->setAltitude(e.text().toDouble());
            } else if (e.tagName() == "ret") {
                ret = e.text().toInt();
            }

            else if(e.tagName() == "HMI")
            {
                Hmi h;
                QDomNodeList hmiList = e.childNodes();
                for (int i = 0; i < hmiList.count(); i++)
                {
                    QDomElement eHmi = hmiList.at(i).toElement();
                    if (eHmi.tagName() == "hmi_busy") {
                        h.setBusy(eHmi.text().toInt());
                    } else if (eHmi.tagName() == "hmi_mode") {
                        h.setMode(eHmi.text().toInt());
                    } else if (eHmi.tagName() == "hmi_state") {
                        h.setState(eHmi.text().toInt());
                    } else if (eHmi.tagName() == "hmi_instruct") {
                        h.setInstruct(eHmi.text().toInt());
                    } else if (eHmi.tagName() == "hmi_allow_auti_swi") {
                        h.setAllowAutiSwi(eHmi.text().toInt());
                    } else if (eHmi.tagName() == "hmi_b4") {
                        h.setB4(eHmi.text().toInt());
                    } else if (eHmi.tagName() == "hmi_reset") {
                        h.setReset(eHmi.text().toInt());
                    } else if (eHmi.tagName() == "hmi_b5") {
                        h.setB5(eHmi.text().toInt());
                    } else if (eHmi.tagName() == "hmi_emrst") {
                        h.setEmrst(eHmi.text().toInt());
                    } else if (eHmi.tagName() == "hmi_b6") {
                        h.setB6(eHmi.text().toInt());
                    } else if (eHmi.tagName() == "hmi_emergency_stop") {
                        h.setEmergencyStop(eHmi.text().toInt());
                    } else if (eHmi.tagName() == "hmi_b7") {
                        h.setB7(eHmi.text().toInt());
                    } else if (eHmi.tagName() == "hmi_enginner_auth") {
                        h.setEnginnerAuth(eHmi.text().toInt());
                    } else if (eHmi.tagName() == "cmd") {
                        h.setCmd(eHmi.text().toInt());
                    } else if (eHmi.tagName() == "x") {
                        h.setX(eHmi.text().toDouble());
                    } else if (eHmi.tagName() == "y") {
                        h.setY(eHmi.text().toDouble());
                    } else if (eHmi.tagName() == "z") {
                        h.setZ(eHmi.text().toDouble());
                    }

                }
                ld->setHmi(h);
            }
        }
        node = node.nextSibling(); // 下一个兄弟节点,nextSiblingElement()是下一个兄弟元素，都差不多
    }


    QDomNodeList list[10];
    QString nodeList[10] = {"work_mode", "cur_trip", "cur_speed", "cur_ei", "cur_power", "cur_mode", "state", "io_input", "limit_spos", "errcode"};

    for (int i = 0; i < 2; i++)
    {
        Motor motor;
        for (int j = 0; j < 10; j++)
        {
            list[j] = doc.elementsByTagName(nodeList[j]);
            QDomElement e = list[j].at(i).toElement();
            switch (j)
            {
            case 0:
                motor.setTrip(e.text().toInt());
                break;
            case 1:
                motor.setSpeed(e.text().toInt());
                break;
            case 2:
                motor.setEi(e.text().toInt());
                break;
            case 3:
                motor.setPower(e.text().toInt());
                break;
            case 4:
                motor.setMode(e.text().toInt());
                break;
            case 5:
                motor.setState(e.text().toInt());
                break;
            case 6:
                motor.setIoInput(e.text().toInt());
                break;
            case 7:
                motor.setLimitSpos(e.text().toInt());
                break;
            case 8:
                motor.setErrcode(e.text().toInt());
                break;
            case 9:
                motor.setWorkMode(e.text().toInt());
                break;
            default:
                break;
            }
        }
        ld->m_motor.append(motor);
    }

    return ret;
}

QString ToolXml::makeSetMotor(QString sn, LocalDev *ld, int m, int mode, int type, double speed, double trip, int power)
{

    QDomDocument doc;
    QDomProcessingInstruction head =
            doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(head);

    // 根节点
    QDomElement tree = doc.createElement("MOTOR_CONTROL_REQ");
    doc.appendChild(tree);

    // 子节点
    tree.appendChild(addElement("sn", sn));
    tree.appendChild(addElement("maddr", ld->getMAddr()));
    tree.appendChild(addElement("busid", QString::number(ld->busID())));
    tree.appendChild(addElement("motor", QString::number(m)));
    tree.appendChild(addElement("work_mode", QString::number(mode)));
    tree.appendChild(addElement("control_type", QString::number(type)));
    tree.appendChild(addElement("speed", QString::number(speed)));
    tree.appendChild(addElement("trip", QString::number(trip)));
    tree.appendChild(addElement("max_power", QString::number(power)));

    return doc.toString();
}

QString ToolXml::makeRebootLocal(QString sn, int busId, QString maddr, ClusterSet_e cmdType)
{
    QDomDocument doc;
    QDomProcessingInstruction head =
            doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(head);

    // 根节点
    QDomElement tree = doc.createElement("LCONTROLLER_CONTROL_REQ");
    doc.appendChild(tree);

    // 子节点
    tree.appendChild(addElement("sn", sn));
    tree.appendChild(addElement("maddr", maddr));
    tree.appendChild(addElement("busid", QString::number(busId)));
    tree.appendChild(addElement("ctype", QString::number(cmdType)));

    return doc.toString();
}

QString ToolXml::makeSetLocalXml(LocalDev *ld)
{
    Hmi h = ld->getHmi();

    QDomDocument doc;
    QDomProcessingInstruction head =
            doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(head);

    // 根节点
    QDomElement tree = doc.createElement("LCONTROLLER_CONFIG_SET_REQ");
    doc.appendChild(tree);

    // 子节点
    tree.appendChild(addElement("sn", ld->getSn()));
    tree.appendChild(addElement("maddr", ld->getMAddr()));
    tree.appendChild(addElement("busid", QString::number(ld->busID())));
    tree.appendChild(addElement("offset", QString::number(ld->offset())));
    tree.appendChild(addElement("smaddr", ld->getSMAddr()));
    tree.appendChild(addElement("baudrate", QString::number(ld->getBaudrate())));
    tree.appendChild(addElement("loglevel", QString::number(ld->getLogLevel())));
    tree.appendChild(addElement("row", QString::number(ld->row())));
    tree.appendChild(addElement("col", QString::number(ld->col())));
    tree.appendChild(addElement("sector", QString::number(ld->sector())));
    tree.appendChild(addElement("hcdi", QString::number(ld->hcdi())));
    tree.appendChild(addElement("longitude", QString::number(ld->getLongitude())));
    tree.appendChild(addElement("latitude", QString::number(ld->getLatitude())));
    tree.appendChild(addElement("altitude", QString::number(ld->getAltitude())));

    // HMI 列表
    QDomElement hmi = doc.createElement("HMI");
    hmi.appendChild(addElement("hmi_busy", QString::number(h.busy())));
    hmi.appendChild(addElement("hmi_mode", QString::number(h.mode())));
    hmi.appendChild(addElement("hmi_state", QString::number(h.state())));
    hmi.appendChild(addElement("hmi_instruct", QString::number(h.instruct())));
    hmi.appendChild(addElement("hmi_allow_auti_swi", QString::number(h.allowAutiSwi())));
    hmi.appendChild(addElement("hmi_b4", QString::number(h.b4())));
    hmi.appendChild(addElement("hmi_reset", QString::number(h.reset())));
    hmi.appendChild(addElement("hmi_b5", QString::number(h.b5())));
    hmi.appendChild(addElement("hmi_emrst", QString::number(h.emrst())));
    hmi.appendChild(addElement("hmi_b6", QString::number(h.b6())));
    hmi.appendChild(addElement("hmi_emergency_stop", QString::number(h.emergencyStop())));
    hmi.appendChild(addElement("hmi_b7", QString::number(h.b7())));
    hmi.appendChild(addElement("hmi_enginner_auth", QString::number(h.enginnerAuth())));
    hmi.appendChild(addElement("cmd", QString::number(h.cmd())));
    hmi.appendChild(addElement("x", QString::number(h.x())));
    hmi.appendChild(addElement("y", QString::number(h.y())));
    hmi.appendChild(addElement("z", QString::number(h.z())));

    tree.appendChild(hmi);

    return doc.toString();
}

QString ToolXml::makeSetClusterXml(Cluster *clus)
{
    QDomDocument doc;
    QDomProcessingInstruction head =
            doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(head);

    // 根节点
    QDomElement tree = doc.createElement("CLUSTER_CONFIG_SET_REQ");
    doc.appendChild(tree);

    // 子节点
    tree.appendChild(addElement("sn", clus->sn()));
    tree.appendChild(addElement("loglevel", QString::number(clus->logLevel())));
    tree.appendChild(addElement("switch", clus->switchId()));
    tree.appendChild(addElement("line", clus->line()));
    tree.appendChild(addElement("line_id", clus->lineId()));
    tree.appendChild(addElement("iptype", QString::number(clus->dhcp())));
    tree.appendChild(addElement("ipaddr", clus->ip()));
    tree.appendChild(addElement("netmask", clus->mask()));
    tree.appendChild(addElement("gateway", clus->gw()));
    tree.appendChild(addElement("dns0", clus->dns1()));
    tree.appendChild(addElement("dns1", clus->dns2()));
    tree.appendChild(addElement("lcnum", QString::number(clus->m_localDev.size())));

    //lclist
    QDomElement lclist = doc.createElement("lclist");

    for (int i = 0; i < clus->m_localDev.size(); i++)
    {
        QDomElement lc = doc.createElement(QString("lc%1").arg(i));

        lc.appendChild(addElement("maddr", clus->m_localDev.at(i).getMAddr()));
        lc.appendChild(addElement("busid", QString::number(clus->m_localDev.at(i).busID())));
        lc.appendChild(addElement("offset", QString::number(clus->m_localDev.at(i).offset())));

        lclist.appendChild(lc);
    }

    tree.appendChild(lclist);

    return doc.toString();
}

QString ToolXml::makeGetCmdXml(LocalDev *ld)
{
    QDomDocument doc;
    QDomProcessingInstruction head =
            doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(head);

    // 根节点
    QDomElement tree = doc.createElement("ALGO_CMD_GET_REQ");
    doc.appendChild(tree);

    // 子节点
    tree.appendChild(addElement("sn", ld->getSn()));
    tree.appendChild(addElement("maddr", ld->getMAddr()));
    tree.appendChild(addElement("busid", QString::number(ld->busID())));

    return doc.toString();
}

int ToolXml::readGetCmdXml(QString xml, localCmd_t * cmd)
{
    int ret = 1;
    QDomDocument doc;
    if(!doc.setContent(xml))
    {
        qDebug() << "xml加载失败！";
        return false;
    }

    //返回根节点
    QDomElement root = doc.documentElement();
    if (root.tagName() != "ALGO_CMD_GET_RES")
    {
        qDebug() << "ALGO_CMD_GET_RES 根节点不匹配！接收到的为：" + root.tagName();
        return 1;
    }

    //获得第一个子节点
    QDomNode node = root.firstChild();
    while(!node.isNull())  // 如果节点不空
    {
        if(node.isElement()) // 如果节点是元素
        {
            QDomElement e = node.toElement(); // 转换为元素，注意元素和节点是两个数据结构，其实差不多
            if (e.tagName() == "ret") {
                ret = e.text().toInt();
            } else if (e.tagName() == "cmd") {
                cmd->cmd = e.text().toUInt();
            } else if (e.tagName() == "x") {
                cmd->x = e.text().toFloat();
            } else if (e.tagName() == "y") {
                cmd->y = e.text().toFloat();
            } else if (e.tagName() == "z") {
                cmd->z = e.text().toFloat();
            }
        }
        node = node.nextSibling(); // 下一个兄弟节点,nextSiblingElement()是下一个兄弟元素，都差不多
    }

    return ret;
}

QString ToolXml::makeSetCmdXml(LocalDev *ld)
{
    QDomDocument doc;
    QDomProcessingInstruction head =
            doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(head);

    // 根节点
    QDomElement tree = doc.createElement("ALGO_CMD_SET_REQ");
    doc.appendChild(tree);

    // 子节点
    localCmd_t cmd = ld->getCmd();
    tree.appendChild(addElement("sn", ld->getSn()));
    tree.appendChild(addElement("maddr", ld->getMAddr()));
    tree.appendChild(addElement("busid", QString::number(ld->busID())));
    tree.appendChild(addElement("cmd", QString::number(cmd.cmd)));
    tree.appendChild(addElement("x", QString::number(cmd.x)));
    tree.appendChild(addElement("y", QString::number(cmd.y)));
    tree.appendChild(addElement("z", QString::number(cmd.z)));

    return doc.toString();
}

QString ToolXml::makeGetDnaXml(LocalDev *ld)
{
    QDomDocument doc;
    QDomProcessingInstruction head =
            doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(head);

    // 根节点
    QDomElement tree = doc.createElement("ALGO_DNA_GET_REQ");
    doc.appendChild(tree);

    // 子节点
    tree.appendChild(addElement("sn", ld->getSn()));
    tree.appendChild(addElement("maddr", ld->getMAddr()));
    tree.appendChild(addElement("busid", QString::number(ld->busID())));

    return doc.toString();
}

int ToolXml::readGetDnaXml(QString xml, localDna_t *dns)
{
    int ret = 1;
    QDomDocument doc;
    if(!doc.setContent(xml))
    {
        qDebug() << "xml加载失败！";
        return false;
    }

    //返回根节点
    QDomElement root = doc.documentElement();
    if (root.tagName() != "ALGO_DNA_GET_RES")
    {
        qDebug() << "ALGO_DNA_GET_RES 根节点不匹配！接收到的为：" + root.tagName();
        return 1;
    }

    //获得第一个子节点
    QDomNode node = root.firstChild();
    while(!node.isNull())  // 如果节点不空
    {
        if(node.isElement()) // 如果节点是元素
        {
            QDomElement e = node.toElement(); // 转换为元素，注意元素和节点是两个数据结构，其实差不多
            if (e.tagName() == "ret") {
                ret = e.text().toInt();
            } else if (e.tagName() == "longitude") {
                dns->longitude = e.text().toFloat();
            } else if (e.tagName() == "latitude") {
                dns->latitude = e.text().toFloat();
            } else if (e.tagName() == "altitude") {
                dns->elevation = e.text().toFloat();
            } else if (e.tagName() == "m01") {
                dns->m_01 = e.text().toFloat();
            } else if (e.tagName() == "m02") {
                dns->m_02 = e.text().toFloat();
            } else if (e.tagName() == "m03") {
                dns->m_03 = e.text().toFloat();
            } else if (e.tagName() == "m04") {
                dns->m_04 = e.text().toFloat();
            } else if (e.tagName() == "m05") {
                dns->m_05 = e.text().toFloat();
            } else if (e.tagName() == "m06") {
                dns->m_06 = e.text().toFloat();
            } else if (e.tagName() == "m07") {
                dns->m_07 = e.text().toFloat();
            } else if (e.tagName() == "m08") {
                dns->m_08 = e.text().toFloat();
            } else if (e.tagName() == "m09") {
                dns->m_09 = e.text().toFloat();
            } else if (e.tagName() == "m10") {
                dns->m_10 = e.text().toFloat();
            } else if (e.tagName() == "m11") {
                dns->m_11 = e.text().toFloat();
            } else if (e.tagName() == "m12") {
                dns->m_12 = e.text().toFloat();
            } else if (e.tagName() == "m13") {
                dns->m_13 = e.text().toFloat();
            } else if (e.tagName() == "m14") {
                dns->m_14 = e.text().toFloat();
            } else if (e.tagName() == "m21") {
                dns->m_21 = e.text().toFloat();
            } else if (e.tagName() == "m21") {
                dns->m_21 = e.text().toFloat();
            } else if (e.tagName() == "m22") {
                dns->m_22 = e.text().toFloat();
            } else if (e.tagName() == "m23") {
                dns->m_23 = e.text().toFloat();
            } else if (e.tagName() == "m24") {
                dns->m_24 = e.text().toFloat();
            } else if (e.tagName() == "m25") {
                dns->m_25 = e.text().toFloat();
            } else if (e.tagName() == "m26") {
                dns->m_26 = e.text().toFloat();
            }
        }
        node = node.nextSibling(); // 下一个兄弟节点,nextSiblingElement()是下一个兄弟元素，都差不多
    }

    return ret;
}

QString ToolXml::makeSetDnaXml(LocalDev *ld)
{
    QDomDocument doc;
    QDomProcessingInstruction head =
            doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(head);

    // 根节点
    QDomElement tree = doc.createElement("ALGO_DNA_SET_REQ");
    doc.appendChild(tree);

    // 子节点
    localDna_t dns = ld->getDna();
    tree.appendChild(addElement("sn", ld->getSn()));
    tree.appendChild(addElement("maddr", ld->getMAddr()));
    tree.appendChild(addElement("busid", QString::number(ld->busID())));
    tree.appendChild(addElement("longitude", QString::number(dns.longitude)));
    tree.appendChild(addElement("latitude", QString::number(dns.latitude)));
    tree.appendChild(addElement("altitude", QString::number(dns.elevation)));
    tree.appendChild(addElement("m01", QString::number(dns.m_01)));
    tree.appendChild(addElement("m02", QString::number(dns.m_02)));
    tree.appendChild(addElement("m03", QString::number(dns.m_03)));
    tree.appendChild(addElement("m04", QString::number(dns.m_04)));
    tree.appendChild(addElement("m05", QString::number(dns.m_05)));
    tree.appendChild(addElement("m06", QString::number(dns.m_06)));
    tree.appendChild(addElement("m07", QString::number(dns.m_07)));
    tree.appendChild(addElement("m08", QString::number(dns.m_08)));
    tree.appendChild(addElement("m09", QString::number(dns.m_09)));
    tree.appendChild(addElement("m10", QString::number(dns.m_10)));
    tree.appendChild(addElement("m11", QString::number(dns.m_11)));
    tree.appendChild(addElement("m12", QString::number(dns.m_12)));
    tree.appendChild(addElement("m13", QString::number(dns.m_13)));
    tree.appendChild(addElement("m14", QString::number(dns.m_14)));
    tree.appendChild(addElement("m21", QString::number(dns.m_21)));
    tree.appendChild(addElement("m22", QString::number(dns.m_22)));
    tree.appendChild(addElement("m23", QString::number(dns.m_23)));
    tree.appendChild(addElement("m24", QString::number(dns.m_24)));
    tree.appendChild(addElement("m25", QString::number(dns.m_25)));
    tree.appendChild(addElement("m26", QString::number(dns.m_26)));

    return doc.toString();
}

/**
 * @brief ToolXml::readMotorRefrXml 更新电机状态的xml解析
 * @param xml xml字符串
 * @param ld 就地控制器指针
 * @return ret
 */
int ToolXml::readMotorRefrXml(QString xml, LocalDev *ld)
{
    bool ok;
    bool result = true;
    QDomDocument doc;
    if(!doc.setContent(xml))
    {
        qDebug() << "xml加载失败！";
        return 1;
    }

    //返回根节点
    QDomElement root = doc.documentElement();
    if (root.tagName() != "MOTOR_STATE_UPDATE_REQ")
    {
        qDebug() << "MOTOR_STATE_UPDATE_REQ 根节点不匹配！接收到的为：" + root.tagName();
        return 1;
    }

    //获得第一个子节点
    QDomElement mc[2];
    QDomNode node = root.firstChild();
    while(!node.isNull())  // 如果节点不空
    {
        if(node.isElement()) // 如果节点是元素
        {
            QDomElement e = node.toElement(); // 转换为元素，注意元素和节点是两个数据结构，其实差不多
            if (e.tagName() == "sn") {
                ld->setSn(e.text());
            } else if (e.tagName() == "maddr") {
                ld->setMAddr(e.text());
            } else if (e.tagName() == "busid") {
                ld->setBusID(e.text().toInt(&ok));
                result = ok ? result : ok;
            } else if (e.tagName() == "mc0") {
                mc[0] = e;
            } else if (e.tagName() == "mc1") {
                mc[1] = e;
            }
        }
        node = node.nextSibling(); // 下一个兄弟节点,nextSiblingElement()是下一个兄弟元素，都差不多
    }

    // 获取电机状态
    ld->m_motor.resize(0);
    for (int i = 0; i < 2; i++)
    {
        Motor motor;
        mc[i].childNodes().size();
        QDomNode node = mc[i].firstChild();
        while(!node.isNull())
        {
            if (node.isElement())
            {
                QDomElement e = node.toElement();

                if (e.tagName() == "cur_trip") {
                    motor.setTrip(e.text().toFloat(&ok));
                    result = ok ? result : ok;
                } else if (e.tagName() == "cur_speed") {
                    motor.setSpeed(e.text().toFloat(&ok));
                    result = ok ? result : ok;
                } else if (e.tagName() == "cur_ei") {
                    motor.setEi(e.text().toFloat(&ok));
                    result = ok ? result : ok;
                } else if (e.tagName() == "cur_power") {
                    motor.setPower(e.text().toFloat(&ok));
                    result = ok ? result : ok;
                } else if (e.tagName() == "cur_mode") {
                    motor.setMode(e.text().toInt());
                } else if (e.tagName() == "state") {
                    motor.setState(e.text().toInt());
                } else if (e.tagName() == "io_input") {
                    motor.setIoInput(e.text().toInt());
                } else if (e.tagName() == "limit_spos") {
                    motor.setLimitSpos(e.text().toInt());
                } else if (e.tagName() == "errcode") {
                    motor.setErrcode(e.text().toInt());
                }
            }
            node = node.nextSibling();
        }
        ld->m_motor.append(motor);
    }

    if (!result)
    {
        qDebug() << "接收到的数据不匹配！";
        return 1;
    }

    return 0;
}

int ToolXml::readRetXml(QString xml)
{
    int ret = 1;
    QDomDocument doc;
    if(!doc.setContent(xml))
    {
        qDebug() << "xml加载失败！";
        return false;
    }

    //返回根节点
    QDomElement root = doc.documentElement();

    //获得第一个子节点
    QDomNode node = root.firstChild();
    while(!node.isNull())  // 如果节点不空
    {
        if(node.isElement()) // 如果节点是元素
        {
            QDomElement e = node.toElement(); // 转换为元素，注意元素和节点是两个数据结构，其实差不多
            if (e.tagName() == "ret")
            {
                ret = e.text().toInt();
            }
        }
        node = node.nextSibling(); // 下一个兄弟节点,nextSiblingElement()是下一个兄弟元素，都差不多
    }

    return ret;
}


int ToolXml::readLoadXml(QString xml, struct ClusterInfo_s info)
{
    int ret;
    QDomDocument doc;
    if(!doc.setContent(xml))
    {
        qDebug() << "xml加载失败！";
        return false;
    }

    //返回根节点
    QDomElement root = doc.documentElement();
    if (root.tagName() != "LOGIN_RES")
    {
        qDebug() << "LOGIN_RES 根节点不匹配！接收到的为：" + root.tagName();
        return 1;
    }

    //获得第一个子节点
    QDomNode node = root.firstChild();
    while(!node.isNull())  // 如果节点不空
    {
        if(node.isElement()) // 如果节点是元素
        {
            QDomElement e = node.toElement(); // 转换为元素，注意元素和节点是两个数据结构，其实差不多
            if (e.tagName() == "ret")
            {
                ret = e.text().toInt();
            }
            else if (e.tagName() == "fw_ver")
            {
                info.fwVer = e.text();
            }
            else if (e.tagName() == "run_time")
            {
                info.runTime = e.text();
            }
            else if (e.tagName() == "desc")
            {
                info.desc = e.text();
            }
        }
        node = node.nextSibling(); // 下一个兄弟节点,nextSiblingElement()是下一个兄弟元素，都差不多
    }

    return ret;
}

/**
 * @brief ToolXml::addElement 创建xml节点
 * @param key   标签
 * @param value 值
 * @return 创建的节点
 */
QDomElement ToolXml::addElement(QString key, QString value)
{
    QDomDocument doc;
    QDomElement element = doc.createElement(key);
    QDomText elementValue = doc.createTextNode(value);
    element.appendChild(elementValue);
    return element;
}

#if 0
void ToolXml::getElement(QDomElement *e, QString tag, void *data, int type)
{
    if (e->tagName() == tag)
    {
        qDebug() << "found!";
        if (type == XML_TYPE_TEXT)
        {
            QString bufS = e->text();
            data = (void *)&bufS;
            qDebug() << bufS;
        }
        else if (type == XML_TYPE_INT)
        {
            int bufI = e->text().toInt();
            data = (void *)&bufI;
            qDebug() << bufI;
        }
    }
}
#endif
